package com.cxg.fairy.ec.main.index.ad;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * Created by CFairy
 */

public class ADGoodsListDataConverter extends DataConverter {

    @Override
    public ArrayList<MultipleItemEntity> convert() {

        final ArrayList<MultipleItemEntity> dataList = new ArrayList<>();
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        final int size = dataArray==null?0:dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);
            final String imageUrl =  data.getString("imageUrl");
            final String mainTitle = data.getString("mainTitle");
            final String subTitle =  data.getString("subTitle");
            final String thumb = data.getString("thumb");
            final String itemType = data.getString("itemType");
            final String price = data.getString("price");
            final String defaultPrice = data.getString("defaultPrice");
            final int id = data.getInteger("skuId")==null?0:data.getInteger("skuId");
            final int spanSize = data.getInteger("spanSize");
            final String cartGoodsName = data.getString("adGoodsName");
            final String unit = data.getString("unit");
            final String adGoodsDescA = data.getString("adGoodsDescriptionA");
            final String adGoodsDescB = data.getString("adGoodsDescriptionB");
            final String header = data.getString("header");

            int type = 0;
            if ("ADVAGOODS".equalsIgnoreCase(itemType)) {
                type = ADGoodsListItemType.AD_GOODS_LIST_VERTICAL_A;
            }else if ("ADHAGOODS".equalsIgnoreCase(itemType)){
                type = ADGoodsListItemType.AD_GOODS_LIST_HORIZONTAL_A;
            }else if ("ADGOODSHEADERA".equalsIgnoreCase(itemType)){
                type = ADGoodsListItemType.AD_GOODS_LIST_HEADER_A;
            }

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.ID, id)
                    .setField(MultipleFields.IMAGE_URL, imageUrl)
                    .setField(ADGoodsListItemFields.HEADER,header)
                    .setField(ADGoodsListItemFields.PRICE, price)
                    .setField(ADGoodsListItemFields.DEFAULT_PRICE, defaultPrice)
                    .setField(ADGoodsListItemFields.AD_GOODS_NAME, cartGoodsName)
                    .setField(ADGoodsListItemFields.UNIT, unit)
                    .setField(ADGoodsListItemFields.POSITION, i)
                    .setField(ADGoodsListItemFields.SPAN_SIZE,spanSize)
                    .setField(ADGoodsListItemFields.THUMB, thumb)
                    .setField(ADGoodsListItemFields.MAIN_TITLE, mainTitle)
                    .setField(ADGoodsListItemFields.SUB_TITLE,subTitle)
                    .setField(ADGoodsListItemFields.AD_GOODS_DESCRIPTION_A,adGoodsDescA)
                    .setField(ADGoodsListItemFields.AD_GOODS_DESCRIPTION_B,adGoodsDescB)
                    .build();

            dataList.add(entity);
        }

        return dataList;
    }

    @Override
    public MultipleItemEntity convertHeader() {
        return null;
    }

    @Override
    public int getVItemCount() {

        final ArrayList<String> vList = new ArrayList<>();
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");

        final int size = dataArray==null?0:dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);
            final String itemType = data.getString("itemType");
            if ("ADVAGOODS".equals(itemType)) {
                vList.add(itemType);
            }
        }

        return vList.size();
    }

}
